﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy;
using TracyGUI.UIData;
using Tracy.DataModel;
using System.Windows.Forms;
using Tracy.Messages;
using Tracy.Handlers;
using SharedLib.BasicServices.MessageFramework;

namespace TracyGUI
{
    public partial class FormMain
    {
        private Dictionary<string, ListViewItem> _mappingResource = new Dictionary<string, ListViewItem>();

        private void AddResourcesToUI(List<Resource> resources)
        {
            foreach (Resource res in resources)
            {
                AddItemToResourceListView(res);
            }
        }

        private void LoadResources()
        {
            ClearResourceListView();
            AddResourcesToUI(TracyBus.Instance.ResourceProvider.GetItems());
        }

        private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DownloadSelectedResources();
        }
        private void removeResourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<Resource> resourcesToRemove = new List<Resource>();
            foreach (ListViewItem item in listViewResources.SelectedItems)
            {
                Resource res = item.Tag as Resource;
                listViewResources.Items.Remove(item);
                if (res != null && !resourcesToRemove.Contains(res))
                {
                    resourcesToRemove.Add(res);
                }
            }

            RemoveResources(resourcesToRemove);
        }

        //TODO: Move to controller, separate it from UI logic
        private void RemoveResources(List<Resource> resourcesToRemove)
        {
            
            List<MediaFile> filesToRemove = new List<MediaFile>();
            foreach(Resource res in resourcesToRemove){
                filesToRemove.AddRange(TracyBus.Instance.MediaFileProvider.GetItemByResource(res));
                _mappingResource.Remove(res.ID);
            }

            RemoveMediaFiles(filesToRemove);
            foreach (Resource res in resourcesToRemove)
            {
                TracyBus.Instance.ResourceProvider.RemoveItem(res.ID);
            }
        }



        private void DownloadSelectedResources()
        {
            foreach (ListViewItem lvSelectedResource in listViewResources.SelectedItems)
            {
                Resource res = lvSelectedResource.Tag as Resource;
                if (res != null)
                {
                    TracyBus.Instance.MessageDispatcher.EnqueueMessage(new TaskMessage(this, typeof(StartDownloadHandler), res, new MessageCallback(OnResourceSentToDownloader)));
                    TracyBus.Instance.MessageDispatcher.EnqueueMessage(new TaskMessage(this, typeof(AssignMediaFileHandler), res, new MessageCallback(OnResourceMediaFilesAssigned)));
                }
            }
        }

        private void ClearResourceListView()
        {
            listViewResources.Items.Clear();
            _mappingResource.Clear();
        }
        private void AddItemToResourceListView(Resource res)
        {
            ListViewItem lvItemResource = new ListViewItem();
            lvItemResource.Name = "Title";
            lvItemResource.Tag = res;
            lvItemResource.Text = res.Title;

            AddSubListViewItem(lvItemResource, "FanSubGroup", FanSubGroup.GetCombinationName(res.FanSubGroups));
            AddSubListViewItem(lvItemResource, "Format", res.Format.Name);
            AddSubListViewItem(lvItemResource, "Resolution", res.Resolution.Name);
            AddSubListViewItem(lvItemResource, "Status", res.Status);
            //AddSubListViewItem(lvItemResource, "URL", res.Url);
            listViewResources.Items.Insert(0, lvItemResource);
            _mappingResource.Add(res.ID, lvItemResource);
        }

        private void OnResourceSentToDownloader(IAppMessage msg)
        {
            
            Resource res = msg.Data as Resource;
            ChangeResourceStatus(res, "Sent");
        }

        private void ChangeResourceStatus(Resource res, string newStatus)
        {
            if (res != null)
            {
                res.Status = newStatus;
                TracyBus.Instance.ResourceProvider.UpdateItem(res);
                ListViewItem lvItemResource = _mappingResource[res.ID];
                lvItemResource.Tag = res;
                lvItemResource.SubItems["Status"].Text = newStatus;
            }
        }
    }
}
